import { useEffect, useState } from "react"


const useRequest = (url: string, options: RequestInit): any => {
  const [data, setData] = useState();
  const [isLoading, setIsLoading] = useState<boolean>(true);
  const [error, setError] = useState();


  useEffect(() => {
    const loadData = async () => {
      setIsLoading(true);
      try {
        let response = await fetch(url, options);
        let data = await response.json();
        setData(data);
      } catch (e: any) {
        setError(e);
      } finally {
        setIsLoading(false);
      }
    }
    loadData();
  }, [url])

  return [data, isLoading, error];
}

export default useRequest